//
//  ViewController.m
//  ChargeHelper
//
//  Created by Coby on 17/9/6.
//  Copyright © 2017年 梦拓科技. All rights reserved.
//

#import "CommitTaskController.h"
#import "TaskWallSystem.h"
#import "ViewController.h"
@interface CommitTaskController ()
@property (nonatomic, weak) IBOutlet UITextField * OnlineTimeTField;
@property (nonatomic, weak) IBOutlet UITextField * manulAreaTField;
@property (nonatomic, weak) IBOutlet UITextField * manuLevelStartTField;
@property (nonatomic, weak) IBOutlet UITextField * manuLevelEndTField;

@property (nonatomic, weak) IBOutlet UIButton * TaskCommitBtn;

@end

@implementation CommitTaskController


-(IBAction)FinaTaskCommit:(id)sender{
    [self saveInputText];
    [self ParentLogStr:@"开始提交任务"];
    if([[TaskWallSystem sharedInstance] rechargetQuestState]==ChargeWaitInput){
        [[TaskWallSystem sharedInstance] ChargeTaskList][@"ChargeHelperState"]=@"3";
        [[[TaskWallSystem sharedInstance] ChargeTaskList] writeToFile:TaskFile  atomically:NO];
        [[TaskWallSystem sharedInstance] setRechargetQuestState:ChargeWaitRequest];
        [self ParentLogStr:@"任务提交参数设置，请等待脚本上传备份数据"];

    }else{
        [self ParentLogStr:@"成功更新提交参数"]; 
    }
    [self.navigationController popViewControllerAnimated:YES];
}

-(IBAction)ReOpenUrl:(id)sender{
    NSLog(@"%@",[[TaskWallSystem sharedInstance] ChargeTaskList][@"data"][@"TaskDescriptionUrl"]);
    
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString: [[TaskWallSystem sharedInstance] ChargeTaskList][@"data"][@"TaskDescriptionUrl"]]];
}

-(void)ParentLogStr:(NSString * )str{
    [(ViewController *)[self.navigationController.viewControllers objectAtIndex: ([self.navigationController.viewControllers count] -2)] LogStr:str];
}

- (void)viewDidLoad {
    NSLog(@"viewDidLoadCommitTaskController");
    [super viewDidLoad];
}

-(void)viewWillAppear:(BOOL)animated{
    [self LoadTextFromTask];
    NSLog(@"viewDidDisappearCOmmitTaskController");
    [super viewWillAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    NSLog(@"viewWillDisappearCommitTaskController");
    [self saveInputText];
    [super viewWillDisappear:animated];
}

-(void)viewDidDisappear:(BOOL)animated{
    NSLog(@"viewDidDisappearCOmmitTaskController");
    [super viewDidDisappear:animated];

}

-(void)LoadTextFromTask{
    if ([[TaskWallSystem sharedInstance] CommitParamaList]!=nil){
        _OnlineTimeTField.text=[[TaskWallSystem sharedInstance] CommitParamaList][@"onlineTime"];
        _manulAreaTField.text=[[TaskWallSystem sharedInstance] CommitParamaList][@"manulArea"];
        _manuLevelStartTField.text=[[TaskWallSystem sharedInstance] CommitParamaList][@"manuLevelStart"];
        _manuLevelEndTField.text=[[TaskWallSystem sharedInstance] CommitParamaList][@"manuLevelEnd"];
    }else if([[TaskWallSystem sharedInstance] ChargeTaskList][@"QStarttime"]){
        NSLog(@"没有任务提交参数缓存记录");
        NSInteger recommandTime=(NSInteger)[[NSDate date] timeIntervalSince1970]-[[[TaskWallSystem sharedInstance] ChargeTaskList][@"QStarttime"] integerValue];
        _OnlineTimeTField.text=[NSString stringWithFormat:@"%ld",(long)recommandTime];
    }
    
    //_ChargeTaskList[@"QStarttime"]=[NSString stringWithFormat:@"%ld", (NSInteger)[[NSDate date] timeIntervalSince1970]];
}

-(void)saveInputText{
    NSMutableDictionary * tempInputTextDictionary=[[NSMutableDictionary alloc] init];
    tempInputTextDictionary[@"onlineTime"]=_OnlineTimeTField.text;
    tempInputTextDictionary[@"manulArea"]=_manulAreaTField.text;
    tempInputTextDictionary[@"manuLevelStart"]=_manuLevelStartTField.text;
    tempInputTextDictionary[@"manuLevelEnd"]=_manuLevelEndTField.text;
    NSLog(@"保存提交任务参数%@",tempInputTextDictionary);
    [[TaskWallSystem sharedInstance] setCommitParamaList:tempInputTextDictionary];
}

//onlineTime,总的在线时间,单位s
//gameArea,游戏所在的服务区,服务器选区的就默认为0
//level,游戏等级
//manulArea 手工输入的区服
//manuLevelStart 开始手工输入的等级
//manuLevelEnd 结束手工输入的等级
@end
